﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMercadoLibre.Models;
using System.IO;

namespace MvcMercadoLibre.Controllers
{
    public class ArticuloController : Controller
    {
        //
        // GET: /Articulo/

        public ActionResult Index()
        {
            Articulo art = new Articulo();
            return View(art.obtenerMisArticulos((int)Session["auth"]));
        }

        //
        // GET: /Articulo/Details/5

        public ActionResult Details(int id)
        {
            return View(new Articulo().obtieneArticulo(id));
        }

        //
        // GET: /Articulo/Create

        public ActionResult Create()
        {
            Articulo art = new Articulo();
            return View(art);
        }

        //
        // POST: /Articulo/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                foreach (string file in Request.Files)
                {
                    var hpf = Request.Files[file] as HttpPostedFileBase;
                    if (hpf.ContentLength == 0)
                    {
                        continue;
                    }
                    var savedFileName = Path.Combine(Server.MapPath("~/Content/Images/Products/"), Path.GetFileName(hpf.FileName));
                    hpf.SaveAs(savedFileName);

                    Articulo articulo = new Articulo { Nombre = collection["Nombre"], Cantidad = Convert.ToInt32(collection["Cantidad"]), Precio = Convert.ToInt32(collection["Precio"]), UrlImagen = Path.GetFileName(hpf.FileName), Vendedor = Session["auth"].ToString() };

                    articulo.guardar();

                    ViewBag.FileUploaded = true;
                }
                // TODO: Add insert logic here
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Articulo/Edit/5

        public ActionResult Edit(int id)
        {

            return View(new Articulo().obtieneArticulo(id));
        }

        //
        // POST: /Articulo/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Articulo oldArticulo = new Articulo().obtieneArticulo(id);
            Articulo newArticulo = new Articulo();
            try
            {
                // TODO: Add update logic here
                newArticulo.Nombre = collection["Nombre"];
                newArticulo.Id = id;
                newArticulo.Precio = Convert.ToInt32(collection["Precio"]);
                newArticulo.Cantidad = Convert.ToInt32(collection["Cantidad"]);
                newArticulo.Vendedor = oldArticulo.Vendedor;

                var hpf = Request.Files["file"] as HttpPostedFileBase;
                if (hpf != null && hpf.ContentLength != 0)
                {
                    var savedFileName = Path.Combine(Server.MapPath("~/Content/Images/Products/"), Path.GetFileName(hpf.FileName));
                    hpf.SaveAs(savedFileName);
                    newArticulo.UrlImagen = Path.GetFileName(hpf.FileName);
                }
                else { newArticulo.UrlImagen = oldArticulo.UrlImagen; }

                newArticulo.actualizar();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(newArticulo);
            }
        }

        //
        // GET: /Articulo/Delete/5

        public ActionResult Delete(int id)
        {
            return View(new Articulo().obtieneArticulo(id));
        }

        //
        // POST: /Articulo/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Articulo art = new Articulo();
            art.Id = id;
            art.Eliminar();
            return RedirectToAction("Index");
        }


    }
}
